package com.roncoo.education.util.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 计算两个日期的时间差值 单位秒
 */
public class TimeDifferUtil {

    public static final String TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";

    /**
     * 计算两个时间 相差多少秒
     * @param time1
     * @param time2
     * @return 秒
     * @throws ParseException
     */
    public static Long differ(String time1,String time2) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_FORMATTER);

        Date date1 = simpleDateFormat.parse(time1);

        Date date2 = simpleDateFormat.parse(time2);

        Long differ = date1.getTime() - date2.getTime();

        Long seconds = Math.abs(differ)/1000;

        return seconds;
    }


    public static String dateToString(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_FORMATTER);
        String format = simpleDateFormat.format(date);
        return format;
    }

//    public static void main(String[] args) throws ParseException {
//        Long differ = differ("2020-4-3 12:57:00", "2020-4-3 12:58:00");
//        System.out.println(differ);
//    }
}
